package com.lzx.chat.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzx.chat.entity.Chat;
import com.lzx.chat.entity.User;
import com.lzx.chat.mapper.ChatMapper;
import com.lzx.chat.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private ChatMapper chatMapper;

    //登录
    @PostMapping("/login")
    public User login(@RequestBody User user) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", user.getUsername());
        User vo = userMapper.selectOne(userQueryWrapper);
        if (vo != null && Objects.equals(vo.getPassword(), user.getPassword())) {
            return vo;
        } else {
            return null;
        }
    }

    //新增聊天记录
    @PostMapping("/add")
    public String add(@RequestBody Chat chat){
        chatMapper.insert(chat);
        return "保存成功";
    }

    //查询所有聊天记录
    @GetMapping("/getList")
    public List<Chat> getList(){
        return chatMapper.selectList(null);
    }
}
